package eu.boss.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Spinner;
import eu.boss.R;

/**
 * This activity is displayed as an alertView (defined in AndroidManifest) to add a new game.
 * @author Arnaud
 *
 */

public class UpdateStatsActivity extends Activity {

	public static String SHADOW="Shadow";
	public static String HUNTER="Hunter";
	public static String NEUTRAL="Neutre";
	private int team=0;
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updatestats);
        
        //Displays the Activity in quasi-full screen
        getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);        
        
		//Initialize components	 
        Spinner spinner = (Spinner)findViewById(R.id.spinner);
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);
        spinnerAdapter.add(SHADOW);
        spinnerAdapter.add(HUNTER);
        spinnerAdapter.add(NEUTRAL);
        spinnerAdapter.notifyDataSetChanged();
        final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        Button btnOk=(Button)findViewById(R.id.btnOk);
        Button btnCancel=(Button)findViewById(R.id.btnCancel);
        
        OnClickListener onClickListenener = new OnClickListener() {
    		/**
    		 * Listener on the buttons
    		 * 
    		 * @param v
    		 *            View (widget) where the user touched
    		 * 
    		 */
    		public void onClick(View v) {

    			if (v.getId()==R.id.btnOk) {
    				//status=0:won; status=1: lost; team=0:shadow; team=1:hunter; team=2:neutral
    				int status;
    				int idChecked=radioGroup.getCheckedRadioButtonId();
    				if (idChecked==R.id.radioWin)
    					status=0;
    				else
    					status=1;
    				Intent intent = new Intent();
					intent.putExtra("gameStatus", status);
					intent.putExtra("team", team);
					setResult(RESULT_OK, intent);    
					finish();
    			}
    			else if (v.getId()==R.id.btnCancel)
    				finish();
    		}
    	};
    	btnOk.setOnClickListener(onClickListenener);
    	btnCancel.setOnClickListener(onClickListenener);
    	
    	spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

			public void onItemSelected(AdapterView<?> parent, View v,
					int position, long id) {
				team=position;
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// Do nothing				
			}
    	});
    }
}
